<template>
  <div style="width: 100%">
    <div class="spec_top">
      <div class="spec_pic">
        <img v-lazy=" commodityInfor.mainPic" style="width: 100%;height: 100%">
      </div>
      <div v-if="goodsType != 6" class="spec-price">
        <span class="prices_text">¥<em>{{showWsPrice}}</em></span>/{{showUnit}}
        <p>库存：{{showStockCount}}{{goodsUnit}}</p>
      </div>

      <div v-if="goodsType == 6" class="spec-price">
        <span class="prices_text">¥<em>{{eachPrice}}</em></span>/{{goodsUnit}}
        (<span style="font-size: 0.6rem">¥</span>{{ showWsPrice + '/' + showUnit }})

        <p>库存：{{showStockCount}}{{goodsUnit}}</p>
      </div>
    </div>

    <div class=" cjy-add-cart-box">
      <span>购买数量</span><span v-if="goodsSpec">(规格：{{goodsSpec}})</span>
      <div style="position: absolute;right: 0.75rem;top: 0;bottom:0">
        <update-cart-for-location v-if="commodityInfor && isLocal"
                                  pageName="detail"
                                  :addCartButtonType="4"
                                  :goodsId="parseInt(goodsId)"
                                  :commodityInfor="commodityInfor"
                                  @choseNum="changeChoseNum"
                                  ref="updateCart"
        ></update-cart-for-location>

        <update-cart v-if="commodityInfor && !isLocal"
                     pageName="detail"
                     :addCartButtonType="4"
                     :goodsId="parseInt(goodsId)"
                     :commodityInfor="commodityInfor"
                     @choseNum="changeChoseNum"
                     ref="updateCart">
        </update-cart>
      </div>
    </div>

    <detail-proce
      v-if="processList"
      :goodsId="goodsId"
      :processList="processList"
      :workingInfo='workingInfo'
      @changeProcess="changeProcess">
    </detail-proce>


  </div>
</template>

<script>
  import UpdateCart from "@/components/UpdateCart";
  import UpdateCartForLocation from "@/components/UpdateCart/CartForOffLine";
  import {queryProcessList} from '../../utils/ReturnAxios'
  import DetailProce from '@/components/DetailProce'

  export default {
    data() {
      return {
        goodsId: '',
        workingInfo: '',
        goodsPrice: 0,
        stockCount: 0,
        goodsUnit: '',
        goodsType: 0,
        showUnit: '',
        image: '',
        processList: null,
        showWsPrice: 0,
        goodsSpec: '',
        choseNum: 0,
        isLocal: false,
        barCode: '',
        showStockCount: 0,
      }
    },
    components: {UpdateCartForLocation, UpdateCart, DetailProce},
    props: {
      commodityInfor: {
        type: Object,
        default: () => {
        }
      },
    },
    methods: {
      async initData() {
        var data = this.commodityInfor
        this.stockCount = data.stockCount
        this.showStockCount = data.showStockCount
        this.goodsUnit = data.goodsUnit
        this.goodsPrice = data.goodsPrice
        this.goodsSpec = data.goodsSpec
        this.goodsId = data.goodsId
        this.workingInfo = this.workingInfo
        this.image = data.mainPic
        this.goodsType = data.goodsType
        this.showUnit = data.showUnit
        this.showWsPrice = data.showWsPrice
        this.barCode = data.barCode
        this.processList = []
        await  this.queryProcessList()
      },
      changeProcess(data) {
        this.workingInfo = data
      },
      updateCart() {
        this.$refs.updateCart && this.$refs.updateCart.changeProcess(this.workingInfo)
      },
      changeChoseNum(data) {
        this.choseNum = data
      },
      async queryProcessList() {
        var res = await queryProcessList(this.commodityInfor.goodsId)
        res = res.data
        // console.log('res ______', res)
        if (res.returnCode) {
          return res.data[0].processlist
        }
        return []
      }
    },
    computed: {
      totalPrice() {
        var data = this.commodityInfor
        var workingInfo = this.workingInfo
        var choseNum = this.choseNum
        var minSaleQty = data.minSaleQty
        var weightUtils = data.weightUtils
        var goodsPrice = data.goodsPrice
        var goodsType = data.goodsType
        var weighingBig = data.weighingBig
        var workPrice = 0
        var totalPrice = 0
        var totalWorkingPrice = 0
        if (choseNum == 0) {
          return 0
        }
        if (workingInfo) {
          workingInfo = JSON.parse(workingInfo)
          workPrice = workingInfo.procemoney
        }
        totalPrice = ((choseNum - 1) * weightUtils + minSaleQty) * goodsPrice
        if (goodsType == 6) {
          totalPrice = totalPrice * weighingBig
        }
        if (goodsType == 6 || goodsType == 7) {
          totalWorkingPrice = workPrice
        } else {
          totalWorkingPrice = choseNum * workPrice
        }
        return totalWorkingPrice + totalPrice
      },
      eachPrice() {
        var goodsType = this.goodsType
        var goodsPrice = this.goodsPrice
        var weighingBig = this.commodityInfor.weighingBig

        if (goodsType == 6) {
          return parseFloat(goodsPrice * weighingBig).toFixed(2)
        }
        return ''
      }

    },
    watch: {
      totalPrice(data) {
      },
    },
    mounted() {
      this.initData()
      console.log(this.commodityInfor, 'data')

    },

  }
</script>

<style>
  .cjy-add-cart-box {
    position: relative;
    width: 100%;
    height: 2.3rem;
    padding: 0 0.5rem;
    line-height: 2.3rem;
    text-align: left;
    vertical-align: middle;
    border-bottom: 1px solid #eaeaea
  }

</style>
